import axios from 'axios';
import authService from './authService';

const API_URL = 'http://localhost:8080/api';

// 辅助函数获取认证头
const getAuthHeader = () => {
  const token = authService.getToken();
  console.log('当前token:', token);
  if (!token) {
    console.warn('未找到认证token');
    return {};
  }
  return {
    'Authorization': `Bearer ${token}`
  };
};

// 获取管理员的所有预约
export const getAllAppointments = async () => {
  console.log(`发送请求到: ${API_URL}/admin/appointments`);
  try {
    const response = await axios.get(`${API_URL}/admin/appointments`, {
      headers: getAuthHeader()
    });
    console.log("预约API响应:", response);
    return response.data;
  } catch (error) {
    console.error("预约API错误:", error);
    throw error;
  }
};

// 根据ID获取预约详情
export const getAppointmentById = async (appointmentId) => {
  try {
    const response = await axios.get(`${API_URL}/admin/appointments/${appointmentId}`, {
      headers: getAuthHeader()
    });
    return response.data;
  } catch (error) {
    console.error(`获取预约ID ${appointmentId} 失败:`, error);
    throw error;
  }
};

// 根据患者ID获取预约
export const getAppointmentsByPatient = async (patientId) => {
  const response = await axios.get(`${API_URL}/admin/appointments/patient/${patientId}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 根据医生ID获取预约
export const getAppointmentsByDoctor = async (doctorId) => {
  const response = await axios.get(`${API_URL}/admin/appointments/doctor/${doctorId}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 医生获取自己的预约列表
export const getDoctorAppointments = async () => {
  try {
    const response = await axios.get(`${API_URL}/doctors/appointments`, {
      headers: getAuthHeader()
    });
    return response.data;
  } catch (error) {
    console.error("获取医生预约列表失败:", error);
    throw error;
  }
};

// 医生更新预约状态
export const updateDoctorAppointmentStatus = async (appointmentId, status) => {
  try {
    const response = await axios.put(`${API_URL}/doctors/appointments/${appointmentId}/status?status=${status}`, {}, {
      headers: getAuthHeader()
    });
    return response.data;
  } catch (error) {
    console.error(`更新预约状态失败:`, error);
    throw error;
  }
};

// 根据科室ID获取预约
export const getAppointmentsByDepartment = async (departmentId) => {
  const response = await axios.get(`${API_URL}/admin/appointments/department/${departmentId}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 根据状态获取预约
export const getAppointmentsByStatus = async (status) => {
  const response = await axios.get(`${API_URL}/admin/appointments/status/${status}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 根据日期获取预约
export const getAppointmentsByDate = async (date) => {
  const response = await axios.get(`${API_URL}/admin/appointments/date/${date}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 根据日期范围获取预约
export const getAppointmentsByDateRange = async (startDate, endDate) => {
  const response = await axios.get(`${API_URL}/admin/appointments/date-range?startDate=${startDate}&endDate=${endDate}`, {
    headers: getAuthHeader()
  });
  return response.data;
};

// 创建新预约
export const createAppointment = async (appointmentData) => {
  const response = await axios.post(`${API_URL}/admin/appointments`, appointmentData, {
    headers: {
      ...getAuthHeader(),
      'Content-Type': 'application/json'
    }
  });
  return response.data;
};

// 更新预约信息
export const updateAppointment = async (appointmentId, appointmentData) => {
  const response = await axios.put(`${API_URL}/admin/appointments/${appointmentId}`, appointmentData, {
    headers: {
      ...getAuthHeader(),
      'Content-Type': 'application/json'
    }
  });
  return response.data;
};

// 更新预约状态
export const updateAppointmentStatus = async (appointmentId, status) => {
  const response = await axios.patch(`${API_URL}/admin/appointments/${appointmentId}/status`, { status }, {
    headers: {
      ...getAuthHeader(),
      'Content-Type': 'application/json'
    }
  });
  return response.data;
};

// 删除预约
export const deleteAppointment = async (appointmentId) => {
  await axios.delete(`${API_URL}/admin/appointments/${appointmentId}`, {
    headers: getAuthHeader()
  });
  return true;
}; 